﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="MAF.Common" %>
<%@ Import Namespace="MAF.Common.Security" %>
<%@ Import Namespace="MAF.UserSecurity" %>
<%@ Import Namespace="Web.UI" %>
<%@ Import Namespace="Domain.Entities" %>
<%@ Import Namespace="log4net" %>
<script runat="server">

    void Application_Start(Object sender, EventArgs e) {
        //log4net.Config.XmlConfigurator.Configure();
        // Code that runs when the application is started.
    }
    
    void Application_End(Object sender, EventArgs e) {
        // Code that runs when the application shuts down.
    }
  
    /// <summary>
    /// 为每个请求授权
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;

        //ILog log = log4net.LogManager.GetLogger("error");
        //log.Debug(DateTime.Now.ToString() + "|" + app.Context.Request.Url);

        string sid = WebHelper.GetCookie(SecurityConst.SID, app.Context);
        if (null != sid)
        {
            //这里是客户端维持登录的关键。
            Authenticator.KeepLogon(new Guid(sid));
        }
    }

    /// <summary>
    /// 重写Url，以适应两个网站位于同一个网址
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;

        string filepath = app.Request.FilePath;
        if (!filepath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
        {
            app.Context.RewritePath("/Default.aspx");
            return;
        }

        string urlA = @"jiruifen.com";
        string urlN0 = @"niuenjian.com";
        string urlN1 = @"nuenjane.com";
        string urlN2 = @"nuenjiane.net";

        bool isRewritePath = true;
        string url = app.Request.Url.ToString();
        string path = "";
        if (-1 != url.IndexOf(urlA))
            path = "Triliph";
        else if (-1 != url.IndexOf(urlN0) || -1 != url.IndexOf(urlN1) || -1 != url.IndexOf(urlN2))
            path = "NuenJane";
        else
            isRewritePath = false;

        if (isRewritePath)
        {
            path = app.Request.Path + "?Version=" + path;
            if (0 < app.Request.QueryString.Count)
                path += "&" + app.Request.QueryString.ToString();

            app.Context.RewritePath(path);
        }      
    }

</script>
